
package horarios;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import net.sourceforge.floggy.persistence.FloggyException;
import net.sourceforge.floggy.persistence.ObjectSet;
import net.sourceforge.floggy.persistence.PersistableManager;
import java.util.Calendar;
import java.util.Date;
/**
 * @author Lais Zanfolim
 */
public class HorariosMIDlet extends MIDlet implements CommandListener, ItemStateListener {

    private boolean midletPaused = false;

    private String filtro = null;
    private String nomeOnibus = null;
    private String origem = null;
    private String destino = null;
    private Date horario = null;
    private String tipo = null;

    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    Date horarioComp = new Date();
    

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command backToList;
    private Command cmdSalvar;
    private Command backCommand;
    private Command exitLista;
    private Command cmdAddHorarios;
    private Command cmdFiltro;
    private Command cmdFiltrar1;
    private Command Filtrar;
    private Command backToList1;
    private Command cmdAddOnibus;
    private Command cmdSalvarOnibus;
    private Command backCommand1;
    private Form addHorario;
    private DateField textFieldHoraSaida;
    private ChoiceGroup choiceTipo;
    private ChoiceGroup popupOnibus;
    private StringItem stringOrigem;
    private Spacer spacer;
    private StringItem stringDestino;
    private Alert alert;
    private List listaHorarios;
    private Form dadosFiltro;
    private ChoiceGroup choiceGroup;
    private Form addOnibus;
    private TextField fieldNomeOnibus;
    private TextField fieldOrigem;
    private TextField fieldDestino;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public HorariosMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getListaHorarios());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == addHorario) {//GEN-BEGIN:|7-commandAction|1|19-preAction
            if (command == backToList) {//GEN-END:|7-commandAction|1|19-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|2|19-postAction
                // write post-action user code here
            } else if (command == cmdSalvar) {//GEN-LINE:|7-commandAction|3|25-preAction
                if(popupOnibus.getString(popupOnibus.getSelectedIndex()).equals("")||stringOrigem.getText().equals("")){
                    getAlert().setString("Algum campo esta vazio! Preencha corretamente");
                }else{
                    adicionarHorario();
                    getAlert().setString("Os dados foram inseridos com sucesso!");
                    popupOnibus.setSelectedIndex(0,true);
                    stringOrigem.setText("");
                    stringDestino.setText("");
                    getChoiceTipo().setSelectedIndex(0,true);
                }
                switchDisplayable(getAlert(), getListaHorarios());//GEN-LINE:|7-commandAction|4|25-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|92-preAction
        } else if (displayable == addOnibus) {
            if (command == backCommand1) {//GEN-END:|7-commandAction|5|92-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|6|92-postAction
                // write post-action user code here
            } else if (command == cmdSalvarOnibus) {//GEN-LINE:|7-commandAction|7|82-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|8|82-postAction
                // write post-action user code here
                if(fieldNomeOnibus.getString().equals("") || fieldOrigem.getString().equals("") || fieldDestino.getString().equals("")){
                    getAlert().setString("Algum campo esta vazio! Preencha corretamente");
                }else{
                    BusManager BM = new BusManager();
                    BM.novoOnibus(getFieldNomeOnibus().getString(), getFieldOrigem().getString(), getFieldDestino().getString());
                    fieldNomeOnibus.setString("");
                    fieldOrigem.setString("");
                    fieldDestino.setString("");
                    getAlert().setString("Os dados foram gravados com sucesso!");
            }
            }//GEN-BEGIN:|7-commandAction|9|30-preAction
        } else if (displayable == alert) {
            if (command == backCommand) {//GEN-END:|7-commandAction|9|30-preAction
                // write pre-action user code here
                switchDisplayable(null, getAddHorario());//GEN-LINE:|7-commandAction|10|30-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|66-preAction
        } else if (displayable == dadosFiltro) {
            if (command == Filtrar) {//GEN-END:|7-commandAction|11|66-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|12|66-postAction
                // write post-action user code here
            } else if (command == backToList1) {//GEN-LINE:|7-commandAction|13|68-preAction
                // write pre-action user code here
                switchDisplayable(null, getListaHorarios());//GEN-LINE:|7-commandAction|14|68-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|35-preAction
        } else if (displayable == listaHorarios) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|15|35-preAction
                // write pre-action user code here
                listaHorariosAction();//GEN-LINE:|7-commandAction|16|35-postAction
                // write post-action user code here

                //listarDados(filtro);
            } else if (command == cmdAddHorarios) {//GEN-LINE:|7-commandAction|17|48-preAction
                // write pre-action user code here
                switchDisplayable(null, getAddHorario());//GEN-LINE:|7-commandAction|18|48-postAction
                // write post-action user code here
            } else if (command == cmdAddOnibus) {//GEN-LINE:|7-commandAction|19|75-preAction
                // write pre-action user code here
                switchDisplayable(null, getAddOnibus());//GEN-LINE:|7-commandAction|20|75-postAction
                // write post-action user code here
            } else if (command == cmdFiltro) {//GEN-LINE:|7-commandAction|21|58-preAction
                // write pre-action user code here
                switchDisplayable(null, getDadosFiltro());//GEN-LINE:|7-commandAction|22|58-postAction
                // write post-action user code here
                //getListaHorarios().deleteAll();
                //filtro = "Norte";


            } else if (command == exitLista) {//GEN-LINE:|7-commandAction|23|38-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|24|38-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|25|7-postCommandAction
        }//GEN-END:|7-commandAction|25|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|26|
    //</editor-fold>//GEN-END:|7-commandAction|26|



    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backToList ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of backToList component.
     * @return the initialized component instance
     */
    public Command getBackToList() {
        if (backToList == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            backToList = new Command("Exit", Command.BACK, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return backToList;
    }
    //</editor-fold>//GEN-END:|18-getter|2|
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: addHorario ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of addHorario component.
     * @return the initialized component instance
     */
    public Form getAddHorario() {
        if (addHorario == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            addHorario = new Form("Novo Hor\u00E1rio de \u00D4nibus", new Item[] { getPopupOnibus(), getStringOrigem(), getStringDestino(), getTextFieldHoraSaida(), getChoiceTipo(), getSpacer() });//GEN-BEGIN:|14-getter|1|14-postInit
            addHorario.addCommand(getBackToList());
            addHorario.addCommand(getCmdSalvar());
            addHorario.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
            addHorario.setItemStateListener(this);
        }//GEN-BEGIN:|14-getter|2|
        return addHorario;
    }
    //</editor-fold>//GEN-END:|14-getter|2|
    //</editor-fold>



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdSalvar ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of cmdSalvar component.
     * @return the initialized component instance
     */
    public Command getCmdSalvar() {
        if (cmdSalvar == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            cmdSalvar = new Command("Inserir", Command.OK, 0);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return cmdSalvar;
    }
    //</editor-fold>//GEN-END:|24-getter|2|







    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            backCommand = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|29-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: alert ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initiliazed instance of alert component.
     * @return the initialized component instance
     */
    public Alert getAlert() {
        if (alert == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            alert = new Alert("Agenda");//GEN-BEGIN:|28-getter|1|28-postInit
            alert.addCommand(getBackCommand());
            alert.setCommandListener(this);
            alert.setTimeout(Alert.FOREVER);//GEN-END:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return alert;
    }
    //</editor-fold>//GEN-END:|28-getter|2|
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitLista ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of exitLista component.
     * @return the initialized component instance
     */
    public Command getExitLista() {
        if (exitLista == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            exitLista = new Command("Voltar", Command.EXIT, 0);//GEN-LINE:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return exitLista;
    }
    //</editor-fold>//GEN-END:|37-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: listaHorarios ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of listaHorarios component.
     * @return the initialized component instance
     */
    public List getListaHorarios() {
        if (listaHorarios == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here

            listaHorarios = new List("Hor\u00E1rios", Choice.IMPLICIT);//GEN-BEGIN:|33-getter|1|33-postInit
            listaHorarios.addCommand(getExitLista());
            listaHorarios.addCommand(getCmdAddHorarios());
            listaHorarios.addCommand(getCmdFiltro());
            listaHorarios.addCommand(getCmdAddOnibus());
            listaHorarios.setCommandListener(this);
            listaHorarios.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            listaHorarios.setSelectCommand(null);//GEN-END:|33-getter|1|33-postInit
            // write post-init user code here
            listaHorarios.deleteAll();
            listarDados(filtro);
        }//GEN-BEGIN:|33-getter|2|
        return listaHorarios;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listaHorariosAction ">//GEN-BEGIN:|33-action|0|33-preAction
    /**
     * Performs an action assigned to the selected list element in the listaHorarios component.
     */
    public void listaHorariosAction() {//GEN-END:|33-action|0|33-preAction
        // enter pre-action user code here
        String __selectedString = getListaHorarios().getString(getListaHorarios().getSelectedIndex());//GEN-LINE:|33-action|1|33-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|33-action|2|
    //</editor-fold>//GEN-END:|33-action|2|
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textFieldHoraSaida ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initiliazed instance of textFieldHoraSaida component.
     * @return the initialized component instance
     */
    public DateField getTextFieldHoraSaida() {
        if (textFieldHoraSaida == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            textFieldHoraSaida = new DateField("Hora de Sa\u00EDda", DateField.TIME);//GEN-BEGIN:|41-getter|1|41-postInit
            textFieldHoraSaida.setPreferredSize(-1, -1);
            textFieldHoraSaida.setDate(new java.util.Date(System.currentTimeMillis()));//GEN-END:|41-getter|1|41-postInit
            // write post-init user code here
        }//GEN-BEGIN:|41-getter|2|
        return textFieldHoraSaida;
    }
    //</editor-fold>//GEN-END:|41-getter|2|
    //</editor-fold>


    //</editor-fold>


    //</editor-fold>


    //</editor-fold>
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdAddHorarios ">//GEN-BEGIN:|47-getter|0|47-preInit
    /**
     * Returns an initiliazed instance of cmdAddHorarios component.
     * @return the initialized component instance
     */
    public Command getCmdAddHorarios() {
        if (cmdAddHorarios == null) {//GEN-END:|47-getter|0|47-preInit
            // write pre-init user code here
            cmdAddHorarios = new Command("Novo Hor\u00E1rio", Command.OK, 0);//GEN-LINE:|47-getter|1|47-postInit
            // write post-init user code here
        }//GEN-BEGIN:|47-getter|2|
        return cmdAddHorarios;
    }
    //</editor-fold>//GEN-END:|47-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceTipo ">//GEN-BEGIN:|52-getter|0|52-preInit
    /**
     * Returns an initiliazed instance of choiceTipo component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceTipo() {
        if (choiceTipo == null) {//GEN-END:|52-getter|0|52-preInit
            // write pre-init user code here
            choiceTipo = new ChoiceGroup("V\u00E1lido em: ", Choice.EXCLUSIVE);//GEN-BEGIN:|52-getter|1|52-postInit
            choiceTipo.append("Dias \u00FAteis", null);
            choiceTipo.append("S\u00E1bados", null);
            choiceTipo.append("Domingos e feriados", null);
            choiceTipo.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            choiceTipo.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|52-getter|1|52-postInit
            // write post-init user code here
        }//GEN-BEGIN:|52-getter|2|
        return choiceTipo;
    }
    //</editor-fold>//GEN-END:|52-getter|2|
    //</editor-fold>
    //</editor-fold>


    //</editor-fold>
    //</editor-fold>
    //</editor-fold>
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdFiltro ">//GEN-BEGIN:|57-getter|0|57-preInit
    /**
     * Returns an initiliazed instance of cmdFiltro component.
     * @return the initialized component instance
     */
    public Command getCmdFiltro() {
        if (cmdFiltro == null) {//GEN-END:|57-getter|0|57-preInit
            // write pre-init user code here
            cmdFiltro = new Command("Filtrar", Command.OK, 0);//GEN-LINE:|57-getter|1|57-postInit
            // write post-init user code here
        }//GEN-BEGIN:|57-getter|2|
        return cmdFiltro;
    }
    //</editor-fold>//GEN-END:|57-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdFiltrar1 ">//GEN-BEGIN:|61-getter|0|61-preInit
    /**
     * Returns an initiliazed instance of cmdFiltrar1 component.
     * @return the initialized component instance
     */
    public Command getCmdFiltrar1() {
        if (cmdFiltrar1 == null) {//GEN-END:|61-getter|0|61-preInit
            // write pre-init user code here
            cmdFiltrar1 = new Command("Ok", Command.OK, 0);//GEN-LINE:|61-getter|1|61-postInit
            // write post-init user code here
        }//GEN-BEGIN:|61-getter|2|
        return cmdFiltrar1;
    }
    //</editor-fold>//GEN-END:|61-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Filtrar ">//GEN-BEGIN:|65-getter|0|65-preInit
    /**
     * Returns an initiliazed instance of Filtrar component.
     * @return the initialized component instance
     */
    public Command getFiltrar() {
        if (Filtrar == null) {//GEN-END:|65-getter|0|65-preInit
            // write pre-init user code here
            Filtrar = new Command("Ok", Command.OK, 0);//GEN-LINE:|65-getter|1|65-postInit
            // write post-init user code here
        }//GEN-BEGIN:|65-getter|2|
        return Filtrar;
    }
    //</editor-fold>//GEN-END:|65-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: dadosFiltro ">//GEN-BEGIN:|63-getter|0|63-preInit
    /**
     * Returns an initiliazed instance of dadosFiltro component.
     * @return the initialized component instance
     */
    public Form getDadosFiltro() {
        if (dadosFiltro == null) {//GEN-END:|63-getter|0|63-preInit
            // write pre-init user code here
            dadosFiltro = new Form("form", new Item[] { getChoiceGroup() });//GEN-BEGIN:|63-getter|1|63-postInit
            dadosFiltro.addCommand(getFiltrar());
            dadosFiltro.addCommand(getBackToList1());
            dadosFiltro.setCommandListener(this);//GEN-END:|63-getter|1|63-postInit
            // write post-init user code here
        }//GEN-BEGIN:|63-getter|2|
        return dadosFiltro;
    }
    //</editor-fold>//GEN-END:|63-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backToList1 ">//GEN-BEGIN:|67-getter|0|67-preInit
    /**
     * Returns an initiliazed instance of backToList1 component.
     * @return the initialized component instance
     */
    public Command getBackToList1() {
        if (backToList1 == null) {//GEN-END:|67-getter|0|67-preInit
            // write pre-init user code here
            backToList1 = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|67-getter|1|67-postInit
            // write post-init user code here
        }//GEN-BEGIN:|67-getter|2|
        return backToList1;
    }
    //</editor-fold>//GEN-END:|67-getter|2|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroup ">//GEN-BEGIN:|72-getter|0|72-preInit
    /**
     * Returns an initiliazed instance of choiceGroup component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGroup() {
        if (choiceGroup == null) {//GEN-END:|72-getter|0|72-preInit
            // write pre-init user code here
            choiceGroup = new ChoiceGroup("choiceGroup", Choice.POPUP);//GEN-BEGIN:|72-getter|1|72-postInit
            choiceGroup.setSelectedFlags(new boolean[] {  });//GEN-END:|72-getter|1|72-postInit
            // write post-init user code here

            choiceGroup.append("Dias \u00FAteis", null);
            choiceGroup.append("S\u00E1bados", null);
            choiceGroup.append("Domingos e feriados", null);
        }//GEN-BEGIN:|72-getter|2|
        return choiceGroup;
    }
    //</editor-fold>//GEN-END:|72-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: popupOnibus ">//GEN-BEGIN:|73-getter|0|73-preInit
    /**
     * Returns an initiliazed instance of popupOnibus component.
     * @return the initialized component instance
     */
    public ChoiceGroup getPopupOnibus() {
        if (popupOnibus == null) {//GEN-END:|73-getter|0|73-preInit
            // write pre-init user code here
            popupOnibus = new ChoiceGroup("Nome do \u00D4nibus", Choice.POPUP);//GEN-BEGIN:|73-getter|1|73-postInit
            popupOnibus.setLayout(ImageItem.LAYOUT_DEFAULT);
            popupOnibus.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            popupOnibus.setSelectedFlags(new boolean[] {  });//GEN-END:|73-getter|1|73-postInit
            // write post-init user code here

        ObjectSet os = null;
        try {
            os = PersistableManager.getInstance().find(Onibus.class, null, null);

            for(int i=0;i<os.size();i++){
                Onibus onibus = (Onibus) os.get(i); //obtem as instancias do banco
                popupOnibus.append(onibus.getNomeOnibus(), null);
            }
        }catch (FloggyException ex){}
        }//GEN-BEGIN:|73-getter|2|
        return popupOnibus;
    }
    //</editor-fold>//GEN-END:|73-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdAddOnibus ">//GEN-BEGIN:|74-getter|0|74-preInit
    /**
     * Returns an initiliazed instance of cmdAddOnibus component.
     * @return the initialized component instance
     */
    public Command getCmdAddOnibus() {
        if (cmdAddOnibus == null) {//GEN-END:|74-getter|0|74-preInit
            // write pre-init user code here
            cmdAddOnibus = new Command("Novo \u00D4nibus", Command.OK, 0);//GEN-LINE:|74-getter|1|74-postInit
            // write post-init user code here
        }//GEN-BEGIN:|74-getter|2|
        return cmdAddOnibus;
    }
    //</editor-fold>//GEN-END:|74-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: cmdSalvarOnibus ">//GEN-BEGIN:|81-getter|0|81-preInit
    /**
     * Returns an initiliazed instance of cmdSalvarOnibus component.
     * @return the initialized component instance
     */
    public Command getCmdSalvarOnibus() {
        if (cmdSalvarOnibus == null) {//GEN-END:|81-getter|0|81-preInit
            // write pre-init user code here
            cmdSalvarOnibus = new Command("Salvar", Command.OK, 0);//GEN-LINE:|81-getter|1|81-postInit
            // write post-init user code here
        }//GEN-BEGIN:|81-getter|2|
        return cmdSalvarOnibus;
    }
    //</editor-fold>//GEN-END:|81-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: addOnibus ">//GEN-BEGIN:|76-getter|0|76-preInit
    /**
     * Returns an initiliazed instance of addOnibus component.
     * @return the initialized component instance
     */
    public Form getAddOnibus() {
        if (addOnibus == null) {//GEN-END:|76-getter|0|76-preInit
            // write pre-init user code here
            addOnibus = new Form("Criar Nova Linha", new Item[] { getFieldNomeOnibus(), getFieldOrigem(), getFieldDestino() });//GEN-BEGIN:|76-getter|1|76-postInit
            addOnibus.addCommand(getCmdSalvarOnibus());
            addOnibus.addCommand(getBackCommand1());
            addOnibus.setCommandListener(this);//GEN-END:|76-getter|1|76-postInit
            // write post-init user code here
        }//GEN-BEGIN:|76-getter|2|
        return addOnibus;
    }
    //</editor-fold>//GEN-END:|76-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fieldNomeOnibus ">//GEN-BEGIN:|78-getter|0|78-preInit
    /**
     * Returns an initiliazed instance of fieldNomeOnibus component.
     * @return the initialized component instance
     */
    public TextField getFieldNomeOnibus() {
        if (fieldNomeOnibus == null) {//GEN-END:|78-getter|0|78-preInit
            // write pre-init user code here
            fieldNomeOnibus = new TextField("Nome do \u00D4nibus", null, 32, TextField.ANY);//GEN-LINE:|78-getter|1|78-postInit
            // write post-init user code here
        }//GEN-BEGIN:|78-getter|2|
        return fieldNomeOnibus;
    }
    //</editor-fold>//GEN-END:|78-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fieldOrigem ">//GEN-BEGIN:|79-getter|0|79-preInit
    /**
     * Returns an initiliazed instance of fieldOrigem component.
     * @return the initialized component instance
     */
    public TextField getFieldOrigem() {
        if (fieldOrigem == null) {//GEN-END:|79-getter|0|79-preInit
            // write pre-init user code here
            fieldOrigem = new TextField("Terminal de Origem", null, 32, TextField.ANY);//GEN-LINE:|79-getter|1|79-postInit
            // write post-init user code here
        }//GEN-BEGIN:|79-getter|2|
        return fieldOrigem;
    }
    //</editor-fold>//GEN-END:|79-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: fieldDestino ">//GEN-BEGIN:|80-getter|0|80-preInit
    /**
     * Returns an initiliazed instance of fieldDestino component.
     * @return the initialized component instance
     */
    public TextField getFieldDestino() {
        if (fieldDestino == null) {//GEN-END:|80-getter|0|80-preInit
            // write pre-init user code here
            fieldDestino = new TextField("Destino", null, 32, TextField.ANY);//GEN-LINE:|80-getter|1|80-postInit
            // write post-init user code here
        }//GEN-BEGIN:|80-getter|2|
        return fieldDestino;
    }
    //</editor-fold>//GEN-END:|80-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer ">//GEN-BEGIN:|88-getter|0|88-preInit
    /**
     * Returns an initiliazed instance of spacer component.
     * @return the initialized component instance
     */
    public Spacer getSpacer() {
        if (spacer == null) {//GEN-END:|88-getter|0|88-preInit
            // write pre-init user code here
            spacer = new Spacer(16, 1);//GEN-LINE:|88-getter|1|88-postInit
            // write post-init user code here
        }//GEN-BEGIN:|88-getter|2|
        return spacer;
    }
    //</editor-fold>//GEN-END:|88-getter|2|
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringOrigem ">//GEN-BEGIN:|89-getter|0|89-preInit
    /**
     * Returns an initiliazed instance of stringOrigem component.
     * @return the initialized component instance
     */
    public StringItem getStringOrigem() {
        if (stringOrigem == null) {//GEN-END:|89-getter|0|89-preInit
            // write pre-init user code here
            stringOrigem = new StringItem("Terminal de Sa\u00EDda", null);//GEN-LINE:|89-getter|1|89-postInit
            // write post-init user code here
             //stringItem.setText();
        }//GEN-BEGIN:|89-getter|2|
        return stringOrigem;
    }
    //</editor-fold>//GEN-END:|89-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringDestino ">//GEN-BEGIN:|90-getter|0|90-preInit
    /**
     * Returns an initiliazed instance of stringDestino component.
     * @return the initialized component instance
     */
    public StringItem getStringDestino() {
        if (stringDestino == null) {//GEN-END:|90-getter|0|90-preInit
            // write pre-init user code here
            stringDestino = new StringItem("Terminal de Destino", null);//GEN-LINE:|90-getter|1|90-postInit
            // write post-init user code here
        }//GEN-BEGIN:|90-getter|2|
        return stringDestino;
    }
    //</editor-fold>//GEN-END:|90-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|91-getter|0|91-preInit
    /**
     * Returns an initiliazed instance of backCommand1 component.
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|91-getter|0|91-preInit
            // write pre-init user code here
            backCommand1 = new Command("Voltar", Command.BACK, 0);//GEN-LINE:|91-getter|1|91-postInit
            // write post-init user code here
        }//GEN-BEGIN:|91-getter|2|
        return backCommand1;
    }
    //</editor-fold>//GEN-END:|91-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

     public void adicionarHorario() {
        Onibus onibus = new Onibus();
        Horario horario = new Horario();

        onibus.setNomeOnibus(popupOnibus.getString(choiceTipo.getSelectedIndex()));
        try {
			onibus.load();
		} catch (FloggyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        horario.setHoraSaida(textFieldHoraSaida.getDate());
        horario.setTipo(choiceTipo.getString(choiceTipo.getSelectedIndex()));

        onibus.adicionaHorario(horario);

        onibus.save();

        /*c1.setTime(objetoHorario.getHorario());
        c2.setTime(horarioComp);
        String time = c1.get(Calendar.HOUR_OF_DAY) + ":" +
        c1.get(Calendar.MINUTE) + ":" +
        c1.get(Calendar.SECOND);
        String time2 = c2.get(Calendar.HOUR_OF_DAY) + ":" +
        c2.get(Calendar.MINUTE) + ":" +
        c2.get(Calendar.SECOND);
        //getAlert().setString(time + "---" + time2);
        if (c1.get(Calendar.HOUR_OF_DAY) == c2.get(Calendar.HOUR_OF_DAY) - 2){
        getAlert().setString(String.valueOf(c1.get(Calendar.MINUTE)));
        }
        else{
        //getAlert().setString("false");
        }*/

    }

    public void adicionarOnibus() {
        Onibus onibus = new Onibus();
        onibus.setNomeOnibus(getFieldNomeOnibus().getString());
        onibus.setOrigem(getFieldOrigem().getString());
        onibus.setDestino(getFieldDestino().getString());
        onibus.save();

    }

    private void listarDados(String filtro) {
        ObjectSet os = null;
        try {
            os = PersistableManager.getInstance().find(Onibus.class, null, null);

            if (os.size() > 0) {
                for (int i = 0; i < os.size(); i++) {
                    Onibus onibus = (Onibus) os.get(i); //obtem as instancias do banco
                    listaHorarios.append(onibus.toString(), null);
                    for (int j = 0; j < onibus.getListaHorarios().size(); j++) {
                        listaHorarios.append(((Horario) onibus.getListaHorarios().elementAt(j)).toString(), null);
                    }
                }
                //if (onibus.getOrigem().equalsIgnoreCase(filtro) || filtro == null ){

            }
        } catch (FloggyException ex) {
            listaHorarios.append("Erro list record", null);
        }
    }

    public void itemStateChanged(Item item) {
        if (item == popupOnibus) {
            Onibus onibus = new Onibus();
            onibus.setNomeOnibus(popupOnibus.getString(popupOnibus.getSelectedIndex()));
            try {
				onibus.load();
			} catch (FloggyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            stringOrigem.setText(onibus.getOrigem());
            stringDestino.setText(onibus.getDestino());
        }
    }
}
